﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 数据库操作会话级对象   ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using Wedn.Net.IDAL;
using Wedn.Net.Model;
using Wedn.Net.Common;
using Wedn.Net.Utility;

namespace Wedn.Net.DALFactory
{
    /// <summary>
    /// 数据库操作会话级对象
    /// </summary>
    public class DbSession : IDbSession
    {
        private readonly string _path = Config.DALAssembly;
        private readonly string _nspace = string.IsNullOrEmpty(Config.DALNamespace) ? Config.DALAssembly : Config.DALNamespace;
        
        private ICommentDAO _comment;
        /// <summary>
        /// CommentDAO 实例
        /// </summary>
		public ICommentDAO CommentDAO
        {
            get
			{
				var className = _nspace + ".CommentDAO";
                if (_comment == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _comment);
                return _comment;
			}
        }

        private ILinkDAO _link;
        /// <summary>
        /// LinkDAO 实例
        /// </summary>
		public ILinkDAO LinkDAO
        {
            get
			{
				var className = _nspace + ".LinkDAO";
                if (_link == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _link);
                return _link;
			}
        }

        private IOptionDAO _option;
        /// <summary>
        /// OptionDAO 实例
        /// </summary>
		public IOptionDAO OptionDAO
        {
            get
			{
				var className = _nspace + ".OptionDAO";
                if (_option == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _option);
                return _option;
			}
        }

        private IPostDAO _post;
        /// <summary>
        /// PostDAO 实例
        /// </summary>
		public IPostDAO PostDAO
        {
            get
			{
				var className = _nspace + ".PostDAO";
                if (_post == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _post);
                return _post;
			}
        }

        private IPostMetaDAO _postmeta;
        /// <summary>
        /// PostMetaDAO 实例
        /// </summary>
		public IPostMetaDAO PostMetaDAO
        {
            get
			{
				var className = _nspace + ".PostMetaDAO";
                if (_postmeta == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _postmeta);
                return _postmeta;
			}
        }

        private IRelationDAO _relation;
        /// <summary>
        /// RelationDAO 实例
        /// </summary>
		public IRelationDAO RelationDAO
        {
            get
			{
				var className = _nspace + ".RelationDAO";
                if (_relation == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _relation);
                return _relation;
			}
        }

        private ITermDAO _term;
        /// <summary>
        /// TermDAO 实例
        /// </summary>
		public ITermDAO TermDAO
        {
            get
			{
				var className = _nspace + ".TermDAO";
                if (_term == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _term);
                return _term;
			}
        }

        private IUserDAO _user;
        /// <summary>
        /// UserDAO 实例
        /// </summary>
		public IUserDAO UserDAO
        {
            get
			{
				var className = _nspace + ".UserDAO";
                if (_user == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _user);
                return _user;
			}
        }

        private IUserMetaDAO _usermeta;
        /// <summary>
        /// UserMetaDAO 实例
        /// </summary>
		public IUserMetaDAO UserMetaDAO
        {
            get
			{
				var className = _nspace + ".UserMetaDAO";
                if (_usermeta == null) AssemblyHelpler.CreateInstance("DbSessionLockHelper", _path, className, ref _usermeta);
                return _usermeta;
			}
        }


		/// <summary>
        /// 保存数据库的改变状态
        /// </summary>
        /// <returns>受影响行数</returns>
        public int SaveChanges()
        {
            var dbContext = CallContext.GetData("CurrentDbContext") as DbContext;
            if (dbContext == null)
            {
                dbContext = new DataModelContainer();
                CallContext.SetData("CurrentDbContext", dbContext);
            }
            //var dbContext = DAL.Factory.GetCurrentDbContext();
            return dbContext.SaveChanges();
        }

		/// <summary>
        /// 获取当前线程内的DbSession对象；
        /// </summary>
		public static IDbSession Instance
        {
            get
            {
                var dbSession = CallContext.GetData("DbSession") as IDbSession;
                if (dbSession == null)
                {
                    dbSession = new DbSession();
                    CallContext.SetData("DbSession", dbSession);
                }
                return dbSession;
            }
        }
    }
} 
